<?php
#   /product/show/avc-2201
class p_show extends page_controllor  
{
    private $_sn;      //产品sn
    
    public function __construct($get_param)
    {
        $this->set_page_id('p_product_show');
        parent::__construct($get_param);
    }
    
    function run()
    {
        $data['notice'] = $this->load_notice();
        
        //接受参数
        $this->_sn = $this->get_input(0);

        if(!is_product_sn($this->_sn))
            $this->not_found();

        //分类，材质，产地
        $oArraytree = new lib_arraytree(array());

        $oArraytree->restart(k::config('product_map'));
        $namefield = $this->__isLangEng ? 'eng_name' : 'name';
        $data['product_map'] = $oArraytree->get_id2field( $namefield );
        
        $oArraytree->restart(k::config('product_class'));
        $namefield = $this->__isLangEng ? 'eng_name' : 'name';
        $data['product_class'] = $oArraytree->get_id2field( $namefield );
        
        $oArraytree->restart(k::config('material'));
        $namefield = $this->__isLangEng ? 'eng_name' : 'name';
        $data['material'] = $oArraytree->get_id2field( $namefield );
        
        
        
        //上一页下一页~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
        $str2show = $this->get_input(1).'/'.$this->get_input(2).'/'.$this->get_input(3);
        //是否支持预订
        $is_multi = $this->get_input(1) == 1 ? 1 : 0;
        if($this->visitor['level'] != TL_USER_LEVEL_VIP)
        {
            $is_multi = 0;
        }
        //日期
        $date = $this->get_input(2);
        
        //根据条件取子分类
        $map_id = (int)$map_id;
        if($map_id)
        {
            $oArraytree->restart(k::config('product_map'));
            $aMapIds = $oArraytree->get_children_id($map_id);
            $aMapIds[] = $map_id;
        }
        $material_id = (int)$material_id;
        if($material_id)
        {
            $oArraytree->restart(k::config('material'));
            $aMaterialIds = $oArraytree->get_children_id($material_id);
            $aMaterialIds[] = $material_id;
        }
        $class_id = (int)$class_id;
        if($class_id)
        {
            $oArraytree->restart(k::config('product_class'));
            $aClassIds = $oArraytree->get_children_id($class_id);
            $aClassIds[] = $class_id;
        }
            
        //取产品信息
        $oProduct = K::load_mod('product');
        $oProduct->get_by_sn($this->_sn)
            or $this->busy();
        $aProduct = $oProduct->get_data();
        if(!$aProduct)
            $this->not_found();
            
        $condition = 'stat='.K_COMMON_YES;
        $condition .= ' AND id<'.$aProduct['id'];
        $condition .= ' AND is_multi='.$is_multi;
        $condition .= $date ? ' AND cdate="'.$date.'"' : '';
        $condition .= $aMapIds ? ' AND map_id in('.implode(',' , $aMapIds).')' : '';
        $condition .= $aMaterialIds ? ' AND material_id in('.implode(',' , $aMaterialIds).')' : '';
        $condition .= $aClassIds ? ' AND class_id in('.implode(',' , $aClassIds).')' : '';
        
        $oProduct->list_product($total , 1 , 1 , $condition) or $this->busy();
        $aProduct_pre = $oProduct->get_data();
        if(!empty($aProduct_pre))
            $data['url_pre'] = '/product/show/'.$aProduct_pre[0]['sn'].'/'.$str2show;
            
        $condition = 'stat='.K_COMMON_YES;
        $condition .= ' AND id>'.$aProduct['id'];
        $condition .= ' AND is_multi='.$is_multi;
        $condition .= $date ? ' AND cdate="'.$date.'"' : '';
        $condition .= $aMapIds ? ' AND map_id in('.implode(',' , $aMapIds).')' : '';
        $condition .= $aMaterialIds ? ' AND material_id in('.implode(',' , $aMaterialIds).')' : '';
        $condition .= $aClassIds ? ' AND class_id in('.implode(',' , $aClassIds).')' : '';
        
        $oProduct->list_product($total , 1 , 1 , $condition , 'id ASC') or $this->busy();
        $aProduct_next = $oProduct->get_data();
        if(!empty($aProduct_next))
            $data['url_next'] = '/product/show/'.$aProduct_next[0]['sn'].'/'.$str2show;
        
        //格式化基本信息
        $aProduct['product_class'] = $data['product_class'][$aProduct['class_id']];
        $aProduct['material'] = $data['material'][$aProduct['material_id']];
        $aProduct['product_map'] = $data['product_map'][$aProduct['map_id']];
        $aProduct['width_inch'] = lib_math::length_cm2inch($aProduct['width']);
        $aProduct['height_inch'] = lib_math::length_cm2inch($aProduct['height']);
        $aProduct['deep_inch'] = lib_math::length_cm2inch($aProduct['deep']);
        $aProduct['cover_url'] = product2coverpath($aProduct['id'] , true).'?'.time();
        $aProduct['cover_big_url'] = product2bigcoverpath($aProduct['id'] , true);
        
        //图片信息
        $data['morepic'] = false;
        if($this->visitor['level'] == TL_USER_LEVEL_VIP)
            $data['morepic'] = true;
        if(count($aProduct['photo_list']) > 0)
        {
            foreach ($aProduct['photo_list'] as $photo_id => $row)
            {
                if($row['is_cover'] == 1 || $data['morepic'])
                {
                    $aImgInfo[] = array(
                        'url' => url_sn2img($this->_sn , $photo_id)
                    );
                }
            }
        }
        
        //当前用户是否已经预订
        $oBuy = K::load_mod('product_buy');
        $oBuy->get_by_uid_pid($this->visitor['uid'] , $aProduct['id']);
        $aOrder = $oBuy->get_data();
        
        unset($aProduct['photo_info']);
        $data['product'] = $aProduct;
        $data['imgs'] = $aImgInfo;
        $data['order'] = $aOrder;
        
        
        
        $this->set_page_title($this->_sn.'_'.$this->__lang['gTitle_p_productList'].'_'.$this->__lang['gTitle_page_title']);
        $this->output('product_show' , $data);
        
    }
}
?>